<template>
  <div>
    <el-form :model="formData" ref="formData" :rules="formRules" label-position="top" class="form-data">

      <el-form-item label="资源名称" prop="name">
        <el-input v-model="formData.name" type="text" placeholder="请输入"/>
      </el-form-item>

      <el-form-item label="资源CODE" prop="code">
        <el-input v-model="formData.code" type="text" placeholder="请输入"/>
      </el-form-item>

      <el-form-item label="资源类型" prop="type">
        <el-input v-model="formData.type" type="text" placeholder="请输入"/>
      </el-form-item>

      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="formData.typeName" type="text" placeholder="请输入"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" type="number" placeholder="请输入"/>
      </el-form-item>
      <el-form-item class="form-button">
        <el-button class="but-view" @click="drawerClose">取 消</el-button>
        <el-button class="but-view" type="primary" @click="onSubmit">提 交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: {
      entity: Object,
      status: String
    },
    name: 'StaticDataForm',
    data() {
      return {
        formData: {
          code: null,
          name: null,
          sort: null,
          type: null,
          typeName: null
        },
        formRules: {
          name: [
            { required: true, message: '请输入资源名称', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入资源CODE', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '请输入排序', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请输入资源类型', trigger: 'blur' }
          ],
          typeName: [
            { required: true, message: '请输入资源类型名称', trigger: 'blur' }
          ]
        }
      }
    },
    beforeMount() {
      if (this.entity) this.getDetails()
    },
    methods: {
      drawerClose() {
        this.$emit('close')
      },
      async onSubmit() {
        this.$refs.formData.validate(async(valid) => {
          if (!valid) return this.$message.warning('请完善信息')
          const url = this.formData.id ? '/resource/table/update' : '/resource/table/add'
          await this.$ajax.post(url, this.formData)
          this.$message.success('操作成功!')
          this.$emit('refresh')
          this.drawerClose()
        })
      },
      /**
       * 获取详情
       */
      async getDetails() {
        const res = await this.$ajax.get(`/resource/table/info?id=${this.entity.id || ''}`)
        this.formData = res.data
      }
    }
  }
</script>

<style scoped>

</style>
